package yf.mall.controller.api.dy;


import com.yf.annotation.IgnoreAuth;
import com.yf.response.R;
import com.yf.response.RRException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.entity.DyAccountAuthEntity;
import yf.mall.entity.KolOrderEntity;
import yf.mall.entity.UserEntity;
import yf.mall.service.NoticeService;
import yf.mall.service.UserService;
import yf.mall.service.app.DyAccountAuthService;
import yf.mall.service.app.KolOrderService;
import yf.mall.utils.StringUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/app/mini/notice")
public class ApiAppDyNoticeController {

    @Resource
    private UserService userService;

    @Resource
    private NoticeService noticeService;

    @RequestMapping("/")
    @IgnoreAuth
    public R index(@RequestParam(required = false) Long userId, @RequestParam(required = false) String info) {
        UserEntity user = userService.get(userId);
        String openId = user.getOpenId();
        List<String> openIds = new ArrayList<>();
        if (StringUtil.isBlank(openId)) {
//            openIds = userService.findAllOpenId();
        } else {
            openIds.add(openId);
        }
        if (StringUtil.isBlank(info)) {
            info = "恭喜成功注册,选爆品用卷卷,让你更快爆单";
        }
        noticeService.sendDyTemp(openIds, info);
        noticeService.sendDyOrder(openId, user.getName(), "4979106559205420473", 1, " 芯婷-9.9/10个-蘑菇卷发器 便携卷发蓬松不伤发硅胶发卷", new Date());
        noticeService.sendDyExpOrder(openId, user.getName(), "产品", 3, new Date());
        return R.ok();
    }


    @Resource
    private DyAccountAuthService authService;

    @Resource
    private KolOrderService kolOrderService;

    @GetMapping("/order")
    @IgnoreAuth
    public R order(@RequestParam(required = false) Long userId) {
        List<DyAccountAuthEntity> auths = authService.getAllAuthByUser(userId);
        if (auths.isEmpty()) {
            throw new RRException("没有授权");
        }
        String openId = auths.get(0).getOpenId();
        KolOrderEntity dyKolOrder =  kolOrderService.findByOpenIdDesc(openId);
        UserEntity user = userService.get(userId);
        noticeService.sendDyOrder(user.getOpenId(), dyKolOrder.getAuthorAccount(), dyKolOrder.getId().toString(), dyKolOrder.getTotalPayAmount().intValue(), dyKolOrder.getProductName(), dyKolOrder.getPaySuccessTime());
        return R.ok();
    }

}
